package ywpt.service.config;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.ClientType;
import ywpt.web.common.PublicFunction;
@Service("clientTypeService")
@RemoteProxy(name="clientTypeService")
public class ClientTypeServiceImpl  extends HibernateEntityDao<ClientType> implements ClientTypeService {
	public  ClientTypeServiceImpl() {
		
	}

	@Override
	public ClientType getClientTypeById(Long id) {
		return this.get(id);
	}

	@Override
	public int getClientTypeCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@Override
	public String CreateTypeCode() {
		DetachedCriteria query = DetachedCriteria.forClass(ClientType.class, "clientType");
		String typeCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<ClientType> clientTypes = this.getClientTypes(query);
		if ( clientTypes.iterator().hasNext() )	
		{
			code = clientTypes.iterator().next().getCode();
			maxId = new Integer(code);
		}
		typeCode=PublicFunction.getNextCode("", maxId, false,3); 
		query.setProjection(null);
		return typeCode;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ClientType> getClientTypes(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public List<ClientType> getClientTypes() {
		return this.getAll();
	}

	@Override
	public void removeClientType(Long id) {
		this.removeById(id);
	}

	@Override
	public void saveClientType(ClientType clientType) {
		this.save(clientType);
	}

	@Override
	public void updateClientType(ClientType clientType) {
		this.update(clientType);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ClientType> getClientTypes(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<ClientType> getClientTypesByTypeName(String type, int start,
			int size) {
		DetachedCriteria query = DetachedCriteria.forClass(ClientType.class, "clientType");
		//query.add(Restrictions.eq("organType", "01"));
		if ( type != null && !type.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("type", "%" + type + "%"),  
							          Restrictions.like("code", "%" + type + "%")));
				// .add(Restrictions.eq("organType", "01"));
		}
		query.addOrder(Order.asc("code"));
		List<ClientType> clientTypeList = new ArrayList<ClientType>();
		List<ClientType> clientTypeLists = new ArrayList<ClientType>();
		ClientType clientType =new ClientType();
		
		clientTypeList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<ClientType> itr = clientTypeList.iterator(); itr.hasNext(); i++ ) 
		{
			clientType = itr.next();
			String ss = PublicFunction.format(clientType.getCode() + "", 6, "  ", 1)
			          //+ PublicFunction.format(organ.getOrganName() + "", 14, "  ", 1);
			          + clientType.getType(); 
			
			clientType.setType(ss);
			clientTypeLists.add(clientType);
		}
		return clientTypeLists;
	}

	@Override
	@RemoteMethod
	public int getClientTypesCountByName(String type) {
		DetachedCriteria query = DetachedCriteria.forClass(ClientType.class, "clientType");
		//query.add(Restrictions.eq("organType", "01"));
		if ( type != null && !type.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("type", "%" + type + "%" ),  
							          Restrictions.like("code", "%" + type + "%" )));
		}
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}
} 
